<template>
	<view class="container">
		<view class="header">
			<text class="title">扫码分享</text>
			<text class="subtitle">扫描下方二维码即可快速分享</text>
		</view>

		<view class="qrcode-container">
			<view class="qrcode-wrapper">
				<image class="qrcode-image"
					:src="image"
					mode="aspectFit" />
			</view>
		</view>

		<view class="action-buttons">
			<button class="save-button" @click="downfile" type="primary">保存二维码</button>
			<button class="copy-button" open-type="share">分享好友</button>
		</view>

		<view class="footer">
			<text class="footer-text">扫描二维码即可分享给好友</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				userinfo: [],
				image:''
			}
		},
		onShow() {
			this.getuserinfo();
			this.getmyfile();
		},

		onShareAppMessage() {

			var user = this.userinfo;
			
			return {
				title: '一起使用',
				path: '/pages/index/index?agent_id=' + user.id
			};
		},

		methods: {

			async getmyfile() {

				var that = this;
				let lists = await this.$api.request('/downs/getqrcode', 'POST');

				console.log('=========', lists)

				that.image = lists;

			},
			async getuserinfo() {

				var that = this;
				let user = await this.$api.request('/user/userinfo', 'POST');

				console.log('=========', user)

				that.userinfo = user;

			},

			downfile() {
				
				var file=this.image;
				uni.downloadFile({
					url: file, // 替换为实际的图片 URL
					success: (res) => {
						if (res.statusCode === 200) {
							// 下载成功，保存图片到相册
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: () => {
									uni.showToast({
										title: '保存成功',
										icon: 'success'
									});
								},
								fail: (err) => {
									uni.showToast({
										title: '保存失败',
										icon: 'error'
									});
								}
							});
						} else {
							uni.showToast({
								title: '下载失败',
								icon: 'error'
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '下载失败',
							icon: 'error'
						});
					}
				});

			}
		}
	}
</script>

<style>
	page {
		height: 100%;
	}

	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		background-color: #ffffff;
		padding: 0 40rpx;
	}

	.header {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 60rpx;
	}

	.title {
		font-size: 22px;
		font-weight: 600;
		color: #333333;
		margin-bottom: 16rpx;
	}

	.subtitle {
		font-size: 14px;
		color: #999999;
	}

	.qrcode-container {
		margin: 40rpx 0;
	}

	.qrcode-wrapper {
		width: 400rpx;
		height: 400rpx;
		background-color: #ffffff;
		border-radius: 16rpx;
		box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 20rpx;
	}

	.qrcode-image {
		width: 360rpx;
		height: 360rpx;
	}

	.action-buttons {
		display: flex;
		flex-direction: column;
		width: 100%;
		margin-top: 60rpx;
	}

	.save-button {
		margin-bottom: 30rpx;
		width: 100%;
	}

	.copy-button {
		width: 100%;
	}

	.footer {
		margin-top: 80rpx;
	}

	.footer-text {
		font-size: 12px;
		color: #999999;
		text-align: center;
	}
</style>